package netty;

/**
 * Created with IDEA
 * author:Yhk
 * Date:2019/6/28
 * Time:9:42 PM
 */

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.net.InetAddress;
import java.util.concurrent.CopyOnWriteArrayList;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    private Log logger = LogFactory.getLog(NettyServerHandler.class);
    final private static CopyOnWriteArrayList list = new CopyOnWriteArrayList();

    //收到数据时调用
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            ByteBuf in = (ByteBuf) msg;
            int readableBytes = in.readableBytes();
            byte[] bytes = new byte[readableBytes];
            in.readBytes(bytes);
            String ii = new String(bytes);
            System.out.print(1);
            //System.out.print(in.toString(CharsetUtil.UTF_8));

        } finally {
            // 抛弃收到的数据
            ReferenceCountUtil.release(msg);
        }
    }

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 当出现异常就关闭连接
        System.out.println(list.size());
        cause.printStackTrace();
        ctx.close();
    }

    /*
     * 建立连接时，返回消息
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //System.out.println("连接的客户端地址:" + ctx.channel().remoteAddress());
//        logger.error("连接的客户端地址:" + ctx.channel().remoteAddress());
//        logger.error("连接的客户端ID:" + ctx.channel().id());
        ctx.writeAndFlush("client" + InetAddress.getLocalHost().getHostName() + "success connected！ \n");
//        System.out.println("connection");
        //StaticVar.ctxList.add(ctx);
        //StaticVar.chc = ctx;
        super.channelActive(ctx);
    }
}
